import * as moment from 'moment';
import formDataService from './formDataService';
import dTalkApi from '../core/dTalkApi';
import visitMonthPlanModel from '../model/visitMonthPlanModel';
import addExportModel from '../model/addExportModel';
import emplModel from '../model/empl2Model';
import dTalkApiUtil from 'dingtalk-app/dtalk/dTalkApiUtil';
import addNewService from '../services/addNewService';
//报表----日均/人均------显示和导出
//钉钉人员每天同步
class AddService{
    constructor(){}

    //报表-1    日均的销售拜访率------------按起止日期计算
    async visitRateByDay(startDate1: string, endDate1: string, comment: string, leaderId: string){
        let dateData=await formDataService.getDate(startDate1,endDate1)
        let startDate=dateData.startDate;
        let endDate=dateData.endDate;
        let num3 =parseInt( moment(startDate).format('DD'));
        let num4 =parseInt( moment(endDate).format('DD'));
        let access_token=await dTalkApiUtil.getAccessToken();
        let authority=await formDataService.getAuthrity(leaderId,access_token)
        let groupName=authority.groupName;
        let leaderDepts = authority.leaderDepts;
        let dataCache: any = []
        let list:any[]=[]
        let userId:any = '';
        let userList: any[] = [];
        let arr=await emplModel.distinct('userId',{})
        if(groupName.includes('全国')||groupName.includes('模块')||groupName.includes('区域')){
            if(leaderDepts.length>0){
                let sonData=await formDataService.getsonDept(leaderDepts,true,access_token,arr);
                let son_list2=sonData.list;
                let sonData2:any={}
                if(groupName.includes('区域')||groupName.includes('模块')){
                    sonData2=await formDataService.getsonDept(leaderDepts,false,access_token,arr)
                }
                let manage_data:any={}
                if(groupName.includes('全国')){
                    manage_data=await addNewService.get_model_area_pro_list(access_token);
                }
                if (groupName.includes('区域')) {//区域负责人
                    if (comment == '省份') {
                        let sonDept=sonData2.listNames
                        if(sonDept.length>0){
                            for (let sonDeptindex = 0; sonDeptindex < sonDept.length; sonDeptindex++) {
                                let pro = sonDept[sonDeptindex];
                                if (!dataCache[pro]) {
                                    dataCache[pro] = {
                                        areaName:pro,
                                        consumerCount:''
                                    }
                                }
                                userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, comment: 1, deptId: { $in:son_list2 }, userId: { $ne: '' }}); 
                                let consumerRate_area=0;//一个省份的日均拜访率
                                let rate:any=0;
                                if(userList.length>0){
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getCountConsumer_one_employeeDate1ToDate2(element2, startDate, endDate);
                                        consumerRate_one_employee=consumerRate_one_employee/(num4-num3+1)
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    rate=(consumerRate_area/userList.length);
                                }
                                dataCache[pro]={
                                    areaName:pro,
                                    consumerCount:rate.toFixed(2)
                                }
                            }
                        }
                    }        
                }
                if(groupName.includes('全国')||groupName.includes('模块')){
                    let son_area_list:any[]=[]//区域部门名称列表
                    let son_province_list:any[]=[]//省份部门名称列表
                    if(groupName.includes('模块')){
                        if(sonData2.listNames.length==0){
                            son_area_list=[];
                            son_province_list=[];
                        }else if(sonData.listNames.length==sonData2.listNames.length&&sonData2.listNames.length>0){
                            son_province_list=[];
                            son_area_list=sonData2.listNames;
                        }else if(sonData.listNames.length !=sonData2.listNames.length&&sonData2.listNames.length>0){
                            son_area_list=sonData2.listNames;
                            sonData.listNames.map(it=>{
                                if(it){
                                    if(!sonData2.listNames.includes(it)){//判断是否是区域部门
                                        if(!son_province_list.includes(it)){//去重
                                            son_province_list.push(it)
                                        }
                                    }
                                }
                            })
                        }
                    }
                    if (comment== '区域') {
                        let areaNameList:any[]=[]
                        if(groupName.includes('模块')){
                            areaNameList= son_area_list
                        }else if(groupName.includes('全国')){
                            areaNameList=await visitMonthPlanModel.distinct("areaName",{areaName:{$in:manage_data.areaNames},comment:1,"$and":[{areaName:{$ne:null}},{areaName:{$ne:''}}]})
                        }
                        if(areaNameList.length>0){
                            for (let index = 0; index < areaNameList.length; index++) {
                                const element = areaNameList[index];
                                if (!dataCache[element]) {
                                    dataCache[element] = {
                                        areaName:element,
                                        consumerCount:''
                                    }
                                }
                                userList = await visitMonthPlanModel.distinct("userId", {areaName:element, deptId: { $in: son_list2 },"$and":[{userId: { $ne: '' }},{userId: { $ne:null}}] });
                                let consumerRate_area=0;//一个区域的日均拜访率
                                let rate:any=0;
                                // console.log(userList.length,'====',element,'=====',consumerRate_area)
                                if(userList.length>0){
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getCountConsumer_one_employeeDate1ToDate2(element2, startDate, endDate);
                                        consumerRate_one_employee=consumerRate_one_employee/(num4-num3+1)
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    rate=(consumerRate_area/userList.length);
                                }
                                dataCache[element]={
                                    areaName:element,
                                    consumerCount:rate.toFixed(2)
                                }
                            }
                        }
                    }
                    if (comment == '省份') {
                        let provinceNameList:any[]=[]
                        if(groupName.includes('模块')){
                            provinceNameList=son_province_list;
                        }else if(groupName.includes('全国')){
                            provinceNameList=await visitMonthPlanModel.distinct("provinceName",{provinceName:{$in:manage_data.proNames},comment:1,"$and":[{provinceName:{$ne:null}},{provinceName:{$ne:''}}]})
                        }
                        if(provinceNameList.length>0){
                            for (let sonDeptindex = 0; sonDeptindex < provinceNameList.length; sonDeptindex++) {
                                let pro = provinceNameList[sonDeptindex];
                                if (!dataCache[pro]) {
                                    dataCache[pro] = {
                                        areaName:pro,
                                        consumerCount:''
                                    }
                                }
                                userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, deptId: { $in:son_list2 }, "$and":[{userId:{$ne:null}},{userId:{$ne:''}}]}); 
                                let consumerRate_area=0;//一个区域的日均拜访率
                                let rate:any=0;
                                if(userList.length>0){
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getCountConsumer_one_employeeDate1ToDate2(element2, startDate, endDate);
                                        consumerRate_one_employee=consumerRate_one_employee/(num4-num3+1)
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    rate=(consumerRate_area/userList.length);
                                }
                                dataCache[pro]={
                                    areaName:pro,
                                    consumerCount:rate.toFixed(2)
                                }
                            }
                        }
                    }
                }
            }
        }
        
        for (const key in dataCache) {
            if (dataCache.hasOwnProperty(key)) {
                const element = dataCache[key];
                list.push(element)
            }

        }
        if(list.length==0){
            list.push({
                areaName:'',
                consumerCount:0
            })
        }
        console.log(list,'list_list_list_list_list_list_list_list_日均拜访率')
        return list;
    }

    //报表-2  人均的客户拜访率------------按起止日期计算
    async consumerRateByEmp(startDate1: string, endDate1: string, comment: string, leaderId: string){
        let dateData=await formDataService.getDate(startDate1,endDate1)
        let startDate=dateData.startDate;
        let endDate=dateData.endDate;
        let num3 =parseInt( moment(startDate).format('DD'));
        let num4 =parseInt( moment(endDate).format('DD'));
        let access_token=await dTalkApiUtil.getAccessToken();
        let authority=await formDataService.getAuthrity(leaderId,access_token)
        let groupName=authority.groupName;
        let leaderDepts = authority.leaderDepts;
        let dataCache: any = []
        let list:any[]=[]
        let userId:any = '';
        let userList: any[] = [];
        let arr=await emplModel.distinct('userId',{})
        if(groupName.includes('全国')||groupName.includes('模块')||groupName.includes('区域')){
            if(leaderDepts.length>0){
                let sonData=await formDataService.getsonDept(leaderDepts,true,access_token,arr);
                let son_list2=sonData.list;
                let sonData2:any={}
                if(groupName.includes('区域')||groupName.includes('模块')){
                    sonData2=await formDataService.getsonDept(leaderDepts,false,access_token,arr)
                }
                let manage_data:any={}
                if(groupName.includes('全国')){
                    manage_data=await addNewService.get_model_area_pro_list(access_token);
                }
                if (groupName.includes('区域')) {//区域负责人
                    if (comment == '省份') {
                        let sonDept=sonData2.listNames
                        if(sonDept.length>0){
                            for (let sonDeptindex = 0; sonDeptindex < sonDept.length; sonDeptindex++) {
                                let pro = sonDept[sonDeptindex];
                                if (!dataCache[pro]) {
                                    dataCache[pro] = {
                                        areaName:pro,
                                        consumerCount:''
                                    }
                                }
                                userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, deptId: { $in:son_list2 }, "$and":[{userId:{$ne:null}},{userId:{$ne:''}}]}); 
                                let consumerRate_area=0;//一个省份的人均拜访率
                                let rate:any=0;
                                if(userList.length>0){
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getCountConsumer_one_employeeDate1ToDate2(element2, startDate, endDate);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    rate=(consumerRate_area/userList.length);
                                }
                                dataCache[pro]={
                                    areaName:pro,
                                    consumerCount:rate.toFixed(0)
                                }
                            }
                        }
                    }        
                }
                if(groupName.includes('全国')||groupName.includes('模块')){
                    let son_area_list:any[]=[]//区域部门名称列表
                    let son_province_list:any[]=[]//省份部门名称列表
                    if(groupName.includes('模块')){
                        if(sonData2.listNames.length==0){
                            son_area_list=[];
                            son_province_list=[];
                        }else if(sonData.listNames.length==sonData2.listNames.length&&sonData2.listNames.length>0){
                            son_province_list=[];
                            son_area_list=sonData2.listNames;
                        }else if(sonData.listNames.length !=sonData2.listNames.length&&sonData2.listNames.length>0){
                            son_area_list=sonData2.listNames;
                            sonData.listNames.map(it=>{
                                if(it){
                                    if(!sonData2.listNames.includes(it)){//判断是否是区域部门
                                        if(!son_province_list.includes(it)){//去重
                                            son_province_list.push(it)
                                        }
                                    }
                                }
                            })
                        }
                    }
                    if (comment== '区域') {
                        let areaNameList:any[]=[]
                        if(groupName.includes('模块')){
                            areaNameList= son_area_list
                        }else if(groupName.includes('全国')){
                            areaNameList=await visitMonthPlanModel.distinct("areaName",{areaName:{$in:manage_data.areaNames},comment:1,"$and":[{areaName:{$ne:null}},{areaName:{$ne:''}}]})
                        }
                        if(areaNameList.length>0){
                            for (let index = 0; index < areaNameList.length; index++) {
                                const element = areaNameList[index];
                                if (!dataCache[element]) {
                                    dataCache[element] = {
                                        areaName:element,
                                        consumerCount:''
                                    }
                                }
                                userList = await visitMonthPlanModel.distinct("userId", {areaName:element, deptId: { $in: son_list2 },"$and":[{userId: { $ne: '' }},{userId: { $ne:null}}] });
                                let consumerRate_area=0;//一个区域的人均拜访率
                                let rate:any=0;
                                if(userList.length>0){
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getCountConsumer_one_employeeDate1ToDate2(element2, startDate, endDate);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    rate=(consumerRate_area/userList.length);
                                }
                                dataCache[element]={
                                    areaName:element,
                                    consumerCount:rate.toFixed(0)
                                }
                            }
                        }
                    }
                    if (comment == '省份') {
                        let provinceNameList:any[]=[]
                        if(groupName.includes('模块')){
                            provinceNameList=son_province_list;
                        }else if(groupName.includes('全国')){
                            provinceNameList=await visitMonthPlanModel.distinct("provinceName",{provinceName:{$in:manage_data.proNames},comment:1,"$and":[{provinceName:{$ne:null}},{provinceName:{$ne:''}}]})
                        }
                        if(provinceNameList.length>0){
                            for (let sonDeptindex = 0; sonDeptindex < provinceNameList.length; sonDeptindex++) {
                                let pro = provinceNameList[sonDeptindex];
                                if (!dataCache[pro]) {
                                    dataCache[pro] = {
                                        areaName:pro,
                                        consumerCount:''
                                    }
                                }
                                userList = await visitMonthPlanModel.distinct("userId", { provinceName:pro, deptId: { $in:son_list2 }, "$and":[{userId: { $ne: '' }},{userId: { $ne:null}}]  }); 
                                let consumerRate_area=0;//一个区域的日均拜访率
                                let rate:any=0;
                                if(userList.length>0){
                                    for (let userindex = 0; userindex < userList.length; userindex++) {
                                        let element2 = userList[userindex];
                                        let consumerRate_one_employee = await formDataService.getCountConsumer_one_employeeDate1ToDate2(element2, startDate, endDate);
                                        consumerRate_area+=consumerRate_one_employee;
                                    }
                                    rate=(consumerRate_area/userList.length);
                                }
                                dataCache[pro]={
                                    areaName:pro,
                                    consumerCount:rate.toFixed(0)
                                }
                            }
                        }
                    }
                }
            }
        }
        for (const key in dataCache) {
            if (dataCache.hasOwnProperty(key)) {
                const element = dataCache[key];
                list.push(element)
            }
        }
        if(list.length==0){
            list.push({
                areaName:'',
                consumerCount:0
            })
        }
        console.log(list,'list_list_list_list_list_list_list_list_人均客户拜访率')
        return list;
    }

    //导出报表-------------日均拜访率
    async exportConsumerCountByDay(startDate1:string,endDate1:string,leaderId:string,comment:string){
        //console.log(startDate1,endDate1,leaderId,comment,'<-------------')
        let dateData=await formDataService.getDate(startDate1,endDate1)
        let startDate=dateData.startDate
        let endDate=dateData.endDate
        let date1 = parseInt(moment(startDate).format('DD'))
        let date2 = parseInt(moment(endDate).format('DD'))
        let access_token=await dTalkApiUtil.getAccessToken();
        let conf:any[]=[]
        let dateList:any[]=[]
        let col:any[]=[]
        col.push('区域名称')
        col.push('部门id')
        for(let index=0;index<=(date2-date1);index++){
            let newDate=moment(startDate).add(index,'days').format('YYYY-MM-DD')
            col.push(newDate+'(日均拜访数量)')
            dateList.push(newDate)
        }
        col.push('日均拜访总数量')
        conf.push(col)
        let authority=await formDataService.getAuthrity(leaderId,access_token);
        let groupName=authority.groupName
        let leaderDepts=authority.leaderDepts
        let arr=await emplModel.distinct('userId',{})
        if(groupName.includes('全国')||groupName.includes('模块')||groupName.includes('区域')){
            if(leaderDepts.length>0){
                let sonData=await formDataService.getsonDept(leaderDepts,true,access_token,arr)
                // let userList=await formDataService.newArray(sonData.userIdData)
                let listNames:any[]=[]  //sonData2.listNames
                let son_area_list2:any[]=[]  //sonData2.dept
                let son_province_list:any[]=[]  
                if(groupName.includes('模块')||groupName.includes('区域')){
                    let sonData2=await formDataService.getsonDept(leaderDepts,false,access_token,arr)
                    if(groupName.includes('区域')){
                        listNames=sonData2.listNames
                    }
                    if(groupName.includes('模块')){
                        if(sonData2.listNames.length==0){
                            listNames=[];
                        }else if(sonData.listNames.length==sonData2.listNames.length&&sonData2.listNames.length>0){
                            listNames=sonData2.listNames
                            son_area_list2=sonData2.dept
                        }else if(sonData.list.length !=sonData2.list.length&&sonData2.list.length>0){
                            listNames=sonData2.listNames;
                            for (let index2 = 0; index2 <sonData.listNames.length; index2++) {
                                let element = sonData.listNames[index2];
                                if(sonData2.listNames.includes(element)){
                                    son_province_list.push('')
                                }else{
                                    son_province_list.push(element)
                                }
                            }
                        }
                    }
                }
                if(comment=='区域'){
                    if(groupName.includes('全国')){
                        listNames=await visitMonthPlanModel.distinct('areaName',{'$and':[{areaName:{$ne:null}},{areaName:{$ne:''}}]})
                    }
                    if(listNames.length>0){
                        for(let listindex=0;listindex<listNames.length;listindex++){
                            let name=listNames[listindex]
                            let m_data:any[]=[]
                            m_data.push(name)
                            let data2=await visitMonthPlanModel.findOne({areaName:name,'$and':[{area:{$ne:null}},{area:{$ne:''}}]})
                            if(data2&&data2.area){
                                m_data.push(data2.area)
                            }else{
                                m_data.push('')
                            }
                            let consumerCount_total:any=0
                            for (let index2 = 0; index2 < dateList.length; index2++) {
                                let date = dateList[index2];
                                let data=await addExportModel.findOne({name_:name,date_:date})
                                consumerCount_total+=data.consumerCount_day
                                if(data){
                                    m_data.push(data.consumerCount_day)
                                }else{
                                    m_data.push('')
                                }
                            }
                            m_data.push(consumerCount_total)
                            conf.push(m_data)
                        }
                    }
                }

                if(comment=='省份'){
                    let areaNameList:any[]=[]
                    if(groupName.includes('全国')){
                        areaNameList=await visitMonthPlanModel.distinct('provinceName',{'$and':[{provinceName:{$ne:null}},{provinceName:{$ne:''}}]})
                    }else{
                        areaNameList=son_province_list    
                    }
                    if(areaNameList.length>0){
                        for (let index4 = 0; index4 < areaNameList.length; index4++) {
                            let pro = areaNameList[index4];
                            if(!pro){
                                continue;
                            }
                            let m_data:any[]=[]
                            m_data.push(name)
                            let data2=await visitMonthPlanModel.findOne({provinceName:pro,'$and':[{province:{$ne:null}},{province:{$ne:''}}]})
                            if(data2&&data2.province){
                                m_data.push(data2.province)
                            }else{
                                m_data.push('')
                            }
                            let consumerCount_total:any=0
                            for (let index5 = 0; index5 < dateList.length; index5++) {
                                let date = dateList[index5];
                                let data=await addExportModel.findOne({name_:pro,date_:date})
                                consumerCount_total+=data.consumerCount_day
                                if(data){
                                    m_data.push(data.consumerCount_day)
                                }else{
                                    m_data.push('')
                                }
                            }
                            m_data.push(consumerCount_total)
                            conf.push(m_data)
                        }
                    }
                }
            }
        }
        console.log(conf,'conf_conf_conf_conf_conf_conf_日均拜访率导出')
        return conf
    }

    //导出报表-------------人均拜访率
    async exportConsumerCountByEmp(startDate1:string,endDate1:string,leaderId:string,comment:string){
        let dateData=await formDataService.getDate(startDate1,endDate1)
        let startDate=dateData.startDate
        let endDate=dateData.endDate
        let date1 = parseInt(moment(startDate).format('DD'))
        let date2 = parseInt(moment(endDate).format('DD'))
        let access_token=await dTalkApiUtil.getAccessToken();
        let conf:any[]=[]
        let dateList:any[]=[]
        let col:any[]=[]
        col.push('区域名称')
        col.push('部门id')
        for(let index=0;index<=(date2-date1);index++){
            let newDate=moment(startDate).add(index,'days').format('YYYY-MM-DD')
            col.push(newDate+'(人均拜访数量)')
            dateList.push(newDate)
        }
        col.push('人均拜访总数量')
        conf.push(col)
        let authority=await formDataService.getAuthrity(leaderId,access_token);
        let groupName=authority.groupName
        let leaderDepts=authority.leaderDepts
        let arr=await emplModel.distinct('userId',{})
        if(groupName.includes('全国')||groupName.includes('模块')||groupName.includes('区域')){
            if(leaderDepts.length>0){
                let sonData=await formDataService.getsonDept(leaderDepts,true,access_token,arr)
                // let userList=await formDataService.newArray(sonData.userIdData)
                let listNames:any[]=[]  //sonData2.listNames
                let son_area_list2:any[]=[]  //sonData2.dept
                let son_province_list:any[]=[]  
                if(groupName.includes('模块')||groupName.includes('区域')){
                    let sonData2=await formDataService.getsonDept(leaderDepts,false,access_token,arr)
                    if(groupName.includes('区域')){
                        listNames=sonData2.listNames
                    }
                    if(groupName.includes('模块')){
                        if(sonData2.listNames.length==0){
                            listNames=[];
                        }else if(sonData.listNames.length==sonData2.listNames.length&&sonData2.listNames.length>0){
                            listNames=sonData2.listNames
                            son_area_list2=sonData2.dept
                        }else if(sonData.list.length !=sonData2.list.length&&sonData2.list.length>0){
                            listNames=sonData2.listNames;
                            for (let index2 = 0; index2 <sonData.listNames.length; index2++) {
                                let element = sonData.listNames[index2];
                                if(sonData2.listNames.includes(element)){
                                    son_province_list.push('')
                                }else{
                                    son_province_list.push(element)
                                }
                            }
                        }
                    }
                }
                if(comment=='区域'){
                    if(groupName.includes('全国')){
                        listNames=await visitMonthPlanModel.distinct('areaName',{'$and':[{areaName:{$ne:null}},{areaName:{$ne:''}}]})
                    }
                    if(listNames.length>0){
                        for(let listindex=0;listindex<listNames.length;listindex++){
                            let name=listNames[listindex]
                            let m_data:any[]=[]
                            m_data.push(name)
                            let data2=await visitMonthPlanModel.findOne({areaName:name,'$and':[{area:{$ne:null}},{area:{$ne:''}}]})
                            if(data2&&data2.area){
                                m_data.push(data2.area)
                            }else{
                                m_data.push('')
                            }
                            let consumerCount_total:any=0
                            if(dateList.length>0){
                                for (let index2 = 0; index2 < dateList.length; index2++) {
                                    let date = dateList[index2];
                                    let data=await addExportModel.findOne({name_:name,date_:date})
                                    if(data&&data.consumerCount_empl){
                                        consumerCount_total+=data.consumerCount_empl
                                    }
                                    if(data){
                                        m_data.push(data.consumerCount_empl)
                                    }else{
                                        m_data.push('')
                                    }
                                }
                            }
                            m_data.push(consumerCount_total)
                            conf.push(m_data)
                        }
                    }
                }
                
                if(comment=='省份'){
                    let areaNameList:any[]=[]
                    if(groupName.includes('全国')){
                        areaNameList=await visitMonthPlanModel.distinct('provinceName',{'$and':[{provinceName:{$ne:null}},{provinceName:{$ne:''}}]})
                    }else{
                        areaNameList=son_province_list    
                    }
                    if(areaNameList.length>0){
                        for (let index4 = 0; index4 < areaNameList.length; index4++) {
                            let pro = areaNameList[index4];
                            if(!pro){
                                continue;
                            }
                            let m_data:any[]=[]
                            m_data.push(pro)
                            let data2=await visitMonthPlanModel.findOne({provinceName:pro,'$and':[{province:{$ne:null}},{province:{$ne:''}}]})
                            if(data2&&data2.province){
                                m_data.push(data2.province)
                            }else{
                                m_data.push('')
                            }
                            let consumerCount_total:any=0
                            for (let index5 = 0; index5 < dateList.length; index5++) {
                                let date = dateList[index5];
                                let data=await addExportModel.findOne({name_:pro,date_:date})
                                consumerCount_total+=data.consumerCount_empl
                                if(data){
                                    m_data.push(data.consumerCount_empl)
                                }else{
                                    m_data.push('')
                                }
                            }
                            m_data.push(consumerCount_total)
                            conf.push(m_data)
                        }
                    }
                }
            }
        }
        console.log(conf,'conf_conf_conf_conf_conf_conf_日均拜访率导出')
        return conf;
    }

    //从钉钉获取人员信息  若是有人员部门的调整,没有人员的离职
    async getEmplFromDD(){
        console.log('获取部门人员开始!!!!!!!!!!',moment().format('YYYY-MM-DD HH:mm:ss'))
        let access_token = await dTalkApiUtil.getAccessToken();
        let data=await dTalkApi.getSondepIdList('1',true,access_token);
        let deptIdList:any=data.department;
        deptIdList.push(1);
        //修改
        let dd_empl_arr:any[]=[];//钉钉部门的人员列表
        let local_empl_arr:any[]=[];//本地的人员列表
      
        local_empl_arr=await emplModel.distinct('userId',{}) ;
        for (let index2 = 0; index2 < deptIdList.length; index2++) {
            let dept_id = deptIdList[index2];
            let mem_data=await dTalkApi.getDeptMembers(dept_id,access_token);
            if(mem_data&&mem_data.errcode==0&&mem_data.userIds){
                mem_data.userIds.map(user_id=>{
                    if(!dd_empl_arr.includes(user_id)){
                        dd_empl_arr.push(user_id)
                    }
                });
            }
        }
        console.log(dd_empl_arr.length,'<=钉钉==本地=>',local_empl_arr.length)
        let date3=moment().format('dddd');
        let error_empl_arr:any[]=[];//钉钉上不存在,但是本地存在的人员    需要删除的人员
        let add_empl_arr:any[]=[];//钉钉上存在,但是本地不存在的人员  需要添加的人员
        if(date3=='Saturday'){
            console.log('今天是星期六,删除全部的旧数据,添加新的数据!!!!!')
            add_empl_arr=dd_empl_arr;
        }else{
            for (let index = 0; index < local_empl_arr.length; index++) {
                let element = local_empl_arr[index];
                if(!dd_empl_arr.includes(element)){
                    error_empl_arr.push(element)
                }
            }
            for (let index3 = 0; index3 < dd_empl_arr.length; index3++) {
                let d = dd_empl_arr[index3];
                if(!local_empl_arr.includes(d)){
                    add_empl_arr.push(d)
                }
            }
            console.log(error_empl_arr.length,'<===需要删除的人员,需要添加的人员===>',add_empl_arr.length)
            let dele=await emplModel.deleteMany({userId:{$in:error_empl_arr}})
            console.log(dele,'<==已经删除的人员',error_empl_arr)
        }
        let newList:any[]=[]
        let oldList=await emplModel.distinct('_id',{})//更新前的旧数据id列表
        let repeatArr:any[]=[];
        if(add_empl_arr.length>0){
            for (let addindex = 0; addindex < add_empl_arr.length; addindex++) {
                let ele2 = add_empl_arr[addindex];
                let emp=await dTalkApi.getUserDetail(ele2,access_token);
                if(emp){
                    let isLeaderInDepts: string = emp.isLeaderInDepts || ''
                    isLeaderInDepts = isLeaderInDepts.substr(1, isLeaderInDepts.length - 2);
                    console.log('添加!!!')
                    if(!repeatArr.includes(ele2)){
                        let b=await emplModel.create({
                            userId: emp.userid,
                            userName: emp.name,
                            userDept:emp.department,
                            avatar:emp.avatar,
                            mobile:emp.mobile,//手机号码
                            tel:emp.tel,//座机电话
                            deptId:emp.department,
                            isLeader:isLeaderInDepts.endsWith(':true'),   //是否是部门主管
                            isAdmin:emp.isAdmin,    //是否是企业管理员
                            isBoss:emp.isBoss,     //是否是企业老板
                            status:'',//用户当前状态
                            job:emp.position,//职位
                            userEmail:emp.email,
                            jobnumber:emp.jobnumber,   //用户工号
                            userArea:emp.workPlace,//用户所在区域
                            timestamp:moment().format('YYYY-MM-DD HH:mm:ss'),
                            comment:1
                        }) 
                        console.log('>>>>>>',emp.name,'<<<<<<',addindex)
                        if(b){
                            if(!newList.includes(b._id)){
                                newList.push(b._id);
                            }
                        }
                        repeatArr.push(ele2)
                    }
                }
            }
            console.log('已经添加的人员==>',add_empl_arr.length,repeatArr.length)
        }
        if(date3=='Saturday'){
            if(oldList.length>0){
                let dele=await emplModel.deleteMany({_id:{$in:oldList}});
                console.log(dele,'dele_dele_dele_dele_删除的旧数据',oldList.length)
            }
        }
        console.log('获取部门人员结束!!!!!!!!!!',moment().format('YYYY-MM-DD HH:mm:ss'))
        return {code:0}
    }






}
let addService=new AddService();
export default addService;